<?php

class Register extends Controller 
{
	var $uid;
	var $tpl;

	function setVars()
	{
		$this->tpl['title'] = "Registration";
		$this->tpl['head'] = '';
        $this->tpl['sidebar'] = '';
		$this->uid = (object) $this->session->userdata('uid');
	}
	
	function Register()
	{
		parent::Controller();
		$this->setVars();
		//$this->output->enable_profiler(TRUE);
		log_message('debug', "Auth->Registration: Controler Class Initialized");
	}

/*****************************************************************\
|               Register User Name
\*****************************************************************/	

	function index() 
	{
		$this->tpl['countryOptions'] = $this->country_options();
		if(isset($_POST['Alias']))
		{
			$this->postCheck();
		}
		else
		{
			$this->load->view('account/register', $this->tpl);
		}
	}

	function postCheck()
	{
		log_message('debug', "Register: Post check");
		$this->load->library('validation');
		$rules['Alias']			= "trim|required|min_length[4]|max_length[20]|xss_clean";
		$rules['eMail']			= "trim|required|valid_email|xss_clean";
		$rules['ZIP']			= "trim|required|xss_clean";
		$rules['Country']		= "trim|required|xss_clean";
		$fields['ZIP']    		= '"ZIP/Post Code"';
		$fields['Country']    	= 'Country';
		$this->validation->set_rules($rules);
		$this->validation->set_fields($fields);
		if ($this->validation->run() == FALSE)
		{
			$this->load->view('account/register', $this->tpl);
		}
		else
		{
			//Check if email exist in db;
			$this->db->select('id, email')->from('contact_people')->where('email', $this->validation->eMail)->limit(1);
			$query = $this->db->get();
			if ($query->num_rows() > 0)
			{
                $people_row = $query->row();
                $sql = "SELECT * FROM unique WHERE id = $people_row->id AND user_role = 1";
                $query = $this->db->query($sql);
                if ($query->num_rows() > 0)
                {
                    $this->msg->setError('You already have an account with us');
                    //$this->msg->setMsg('We already have emailed you your account information.');
                    //$this->Account->SendPass($query->alias, $email, $query->pass);
                    $this->load->view('account/register', $this->tpl);
                }
                else
                {
                    $this->uid = $people_row->id;
                    $user_data = $this->user_create();
                    $this->unique_create();
                    $people_data = (array) $people_row;
                    $this->session_set($people_data, $user_data);
                    redirect('account/secquestion', 'location');                }
			}else{
                $this->uid = $this->fun->dbid();
                $user_data = $this->user_create();
				$people_data = array(
                    'id' => $this->uid,
                    'email' 	=> $this->validation->eMail,
                    'country_fips'=> $this->Country,
                    'postcode' 	=> $this->validation->ZIP
                );
                $this->db->insert('contact_people', $people_data);
                $this->unique_create();
                $this->session_set($people_data, $user_data);
				redirect('account/secquestion', 'location');
			}
		}
	}
	
    
    function user_create()
    {
        $this->load->helper('string');
        $CTZ = $this->session->userdata('CTZ');
        $this->Country = $CTZ[$this->validation->Country]['FIPS'];
        $TZ = $CTZ[$this->validation->Country]['TZ_Linux'];
        $user_data = array(
            'id'    => $this->uid,
            'alias' => $this->validation->Alias,
            'pass' => random_string('alnum', 6),
            'timezone' => $TZ,
            'IP' => $this->fun->getip(),
            'first' => date("Y-m-d H:i:s") # We are in GMT timezone no need for gmdate
            );
        $this->db->insert('contact_user', $user_data); 
        
        return $user_data;
    }
    
    function unique_create()
    {
        $unique = array(
                'id' => $this->uid,
                'user_role' => 1
                );
        $this->db->insert('unique', $unique);
    }

    function session_set($people_data, $user_data)
    {
        $data =  array_merge($people_data, $user_data);
        $data['logged_in'] = FALSE;
        $this->session->set_userdata($data);
        $this->session->unset_userdata('CTZ');
    }

    
    function country_options()
	{
		$output = '<select name="Country">';
		$output .= "<option value=\"\">--Location by Timezone--</option>\n";
		$tz = $this->session->userdata('TZ');
		if(isset($tz))
		{
			$sql = "
			SELECT places_timezone.id, FIPS, TZ_Linux, Country 
			FROM places_timezone
			JOIN (places_country) 
			ON (places_timezone.ISO3166 = places_country.ISO3166)
			WHERE TZ = '$tz'
			";
		}
		else
		{
			$sql = "
			SELECT places_timezone.id, FIPS, TZ_Linux, Country 
			FROM places_timezone
			JOIN (places_country) 
			ON (places_timezone.ISO3166 = places_country.ISO3166)
			";
		}
		
		$query = $this->db->query($sql);
		foreach ($query->result() as $row)
		{
			if(isset($tz))
			{
				$this->TZ_Linux = $row->TZ_Linux;
				$d['CTZ'][$row->id]['TZ_Linux'] = $row->TZ_Linux;
				$d['CTZ'][$row->id]['FIPS'] = $row->FIPS;
				$this->session->set_userdata($d);
				$l = strstr($row->TZ_Linux, '/');
				$c = $row->Country;
				$c = $c.$l;
			}
			$output .= "<option value=\"$row->id\"";
			$output .= ">$c</option>\n";
		}
		$output .= '</select>'; 
		return $output;		
	}
}
?>
